package com.tsing.wms.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tsing.wms.entity.GoodsEntity;
import com.tsing.wms.entity.RecordDetailEntity;
import com.tsing.wms.entity.StockEntity;
import com.tsing.wms.entity.SupplierEntity;
import com.tsing.wms.exception.CommonException;
import com.tsing.wms.exception.ServerException;
import com.tsing.wms.mapper.GoodsMapper;
import com.tsing.wms.mapper.RecordDetailMapper;
import com.tsing.wms.mapper.StockMapper;
import com.tsing.wms.mapper.SupplierMapper;
import com.tsing.wms.service.IGoodsService;
import com.tsing.wms.vo.PageResultVO;
import com.tsing.wms.vo.req.GoodsReqVO;
import com.tsing.wms.vo.req.IdReqVO;
import com.tsing.wms.vo.req.IdsReqVO;
import com.tsing.wms.vo.req.SupplierPageReqVO;
import com.tsing.wms.vo.resp.GoodsRespVO;
import com.tsing.wms.vo.resp.IdAndNameRespVO;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 货物表 服务实现类
 * </p>
 *
 * @author TheTsing
 * @since 2022-04-13
 */
@Service
public class GoodsServiceImpl extends ServiceImpl<GoodsMapper, GoodsEntity> implements IGoodsService {

    @Resource
    private SupplierMapper supplierMapper;

    @Override
    public List<IdAndNameRespVO> supplierList() {
        List<SupplierEntity> supplierEntities = supplierMapper.selectList(null);
        return BeanUtil.copyToList(supplierEntities, IdAndNameRespVO.class);
    }

    @Override
    public void addGoods(GoodsReqVO reqVO) {
        if (ObjectUtil.isNotNull(new LambdaQueryChainWrapper<>(baseMapper)
                .select(GoodsEntity::getId)
                .eq(GoodsEntity::getName, reqVO.getName())
                .eq(GoodsEntity::getSupplierId, reqVO.getSupplierId())
                .one())) {
            throw new ServerException(CommonException.Proxy.GOODS_IS_NOTNULL);
        }
        GoodsEntity goodsEntity = new GoodsEntity();
        BeanUtil.copyProperties(reqVO, goodsEntity);
        baseMapper.insert(goodsEntity);
    }

    @Resource
    private StockMapper stockMapper;
    @Resource
    private RecordDetailMapper recordDetailMapper;

    @Override
    public void deleteGoods(IdsReqVO reqVO) {
        for (String id : reqVO.getIds()) {
            if (0 != new LambdaQueryChainWrapper<>(stockMapper)
                    .select(StockEntity::getId)
                    .eq(StockEntity::getGoodsId, id)
                    .count()) {
                throw new ServerException(CommonException.Proxy.GOODS_RELEVANCE_STOCK);
            }
            if (0 != new LambdaQueryChainWrapper<>(recordDetailMapper)
                    .select(RecordDetailEntity::getId)
                    .eq(RecordDetailEntity::getGoodsId, id)
                    .count()) {
                throw new ServerException(CommonException.Proxy.GOODS_RELEVANCE_RECORD_DETAIL);
            }
        }
        baseMapper.deleteBatchIds(reqVO.getIds());
    }

    @Override
    public GoodsRespVO getGoods(IdReqVO reqVO) {
        GoodsEntity goodsEntity = baseMapper.selectById(reqVO.getId());
        GoodsRespVO goodsRespVO = new GoodsRespVO();
        BeanUtil.copyProperties(goodsEntity, goodsRespVO);
        goodsRespVO.setSupplierName(new LambdaQueryChainWrapper<>(supplierMapper)
                .select(SupplierEntity::getName)
                .eq(SupplierEntity::getId, goodsRespVO.getSupplierId())
                .one()
                .getName());
        return goodsRespVO;
    }

    @Override
    public void updateGoods(GoodsReqVO reqVO) {
        if (StrUtil.isNotBlank(reqVO.getName())) {
            if (StrUtil.isNotBlank(reqVO.getSupplierId())) {
                if (ObjectUtil.isNotNull(new LambdaQueryChainWrapper<>(baseMapper)
                        .select(GoodsEntity::getId)
                        .eq(GoodsEntity::getName, reqVO.getName())
                        .eq(GoodsEntity::getSupplierId, reqVO.getSupplierId())
                        .ne(GoodsEntity::getId, reqVO.getId())
                        .one())) {
                    throw new ServerException(CommonException.Proxy.GOODS_IS_NOTNULL);
                }
            } else {
                if (ObjectUtil.isNotNull(new LambdaQueryChainWrapper<>(baseMapper)
                        .select(GoodsEntity::getId)
                        .eq(GoodsEntity::getName, reqVO.getName())
                        .eq(GoodsEntity::getSupplierId, baseMapper.selectById(reqVO.getId()).getSupplierId())
                        .ne(GoodsEntity::getId, reqVO.getId())
                        .one())) {
                    throw new ServerException(CommonException.Proxy.GOODS_IS_NOTNULL);
                }
            }
        }
        GoodsEntity goodsEntity = new GoodsEntity();
        BeanUtil.copyProperties(reqVO, goodsEntity);
        baseMapper.updateById(goodsEntity);
    }

    @Override
    public PageResultVO<GoodsRespVO> goodsPage(SupplierPageReqVO reqVO) {
        IPage<GoodsEntity> page = new Page<>(reqVO.getPageNum(), reqVO.getPageSize());
        new LambdaQueryChainWrapper<>(baseMapper)
                .like(StrUtil.isNotBlank(reqVO.getName()), GoodsEntity::getName, reqVO.getName())
                .page(page);
        List<GoodsRespVO> goodsRespVOs = BeanUtil.copyToList(page.getRecords(), GoodsRespVO.class);
        goodsRespVOs.forEach(item -> item.setSupplierName(new LambdaQueryChainWrapper<>(supplierMapper)
                .select(SupplierEntity::getName)
                .eq(SupplierEntity::getId, item.getSupplierId())
                .one()
                .getName()));
        return new PageResultVO<>(page, goodsRespVOs);
    }

}
